package com.roncoo.education.db.service;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.roncoo.education.api.request.ProductRequest;
import com.roncoo.education.api.response.ProductImageBean;
import com.roncoo.education.api.response.ProductResponse;
import com.roncoo.education.db.dao.ProductDao;
import com.roncoo.education.db.entity.Product;
import com.roncoo.education.db.entity.ProductImage;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ProductService {

    @Resource
    private ProductDao productDao;

    @Resource
    private ProductImageService productImageService;

    public Map<String,String> saveProduct(Long adminId, ProductRequest req){
        Product product = BeanUtil.copyProperties(req,Product.class);
        Long pid = product.getId();
        Map<String,String> ret = new HashMap<>();
        Product query = new Product();
        query.setProName(req.getProName());
        List<Product> list = productDao.findByBean(query);
        if(CollUtil.isNotEmpty(list)){
            if(product.getId()!=null){
                for (Product product1 : list) {
                    if(!product1.getId().equals(product.getId())){
                        ret.put("isError", "The name already exists");
                        ret.put("code", "998");
                        return ret;
                    }
                }
            }else {
                ret.put("isError", "The name already exists");
                ret.put("code", "998");
                return ret;
            }
        }

        if(product.getId()!=null){
            product.setUpdateTime(new Date());
            product.setUpId(adminId);
            productDao.updateById(product);
        }else{

            product.setCreateTime(new Date());
            product.setCuId(adminId);
            product.setDelFlag(1);
            productDao.insert(product);
            pid = product.getId();
        }
        if(pid!=null) {
            productImageService.saveImage(pid,req.getImages());
        }
        ret.put("isError", "success");
        ret.put("id", String.valueOf(pid));
        ret.put("code", "1");
        return ret;
    }

//    public Product getProductById(Long id){
//        return productDao.findById(id);
//    }

    public ProductResponse getProductById(Long id){
        Product product = productDao.findById(id);
        if(product!=null) {
            ProductResponse response = BeanUtil.copyProperties(product, ProductResponse.class);
            List<ProductImage> images = productImageService.getImagesByProId(id);
            List<ProductImageBean> imageList = BeanUtil.copyToList(images, ProductImageBean.class);
            response.setImages(imageList);
            return response;
        }
        return null;
    }

    public void getProdAddViews(Long id,Integer views){
        Product update = new Product();
        update.setId(id);
        if(views==null){
            update.setViews(1);
        }else {
            update.setViews(views + 1);
        }
        productDao.updateById(update);
    }

    public PageInfo<Product> getProdPages(ProductRequest req){
        int pageNum = req.getPageNum();
        int pageSize = req.getCount();
        if (req.getPageNum() == null || req.getPageNum() <= 0 || req.getCount() == null) {
            pageNum = 1;
            pageSize = 20;
        }
        Product query = new Product();
        query.setProName(req.getProName());
        query.setDelFlag(1);
        PageHelper.startPage(pageNum, pageSize);
        List<Product> newsList = productDao.getProdsMainImage(query);
        return new PageInfo<>(newsList);
    }

    public List<Product> getProdsList(Integer saleFlag){
        Product query = new Product();
        query.setDelFlag(1);
        query.setSlaeFlag(saleFlag);
        List<Product> list = productDao.getProdsMainImage(query);
        return list;
    }

    public void setDelFlag(Long id){
        Product query = productDao.findById(id);
        if(query!=null){
            Product update = new Product();
            update.setId(query.getId());
            update.setDelFlag(0);
            productDao.updateById(update);
        }
    }

    public void deleteProduct(Long id){
        Product query = productDao.findById(id);
        if(query!=null){
            productDao.deleteById(query);
        }
    }
}
